
package com.android.finaltracer.model;

import org.json.JSONException;
import org.json.JSONObject;

public class SyncInfo extends DataFormatBasic {
    public final static int SYNC_HANDSHAKE_REQ = 1;
    public final static int SYNC_HANDSHAKE_CNF = SYNC_HANDSHAKE_REQ + 1;

    private static final String TAG_COMMAND = "command";
    private static final String TAG_MODULE = "module";
    private static final String TAG_STATUS = "status";
    private static final String TAG_ALIVE = "alive";
    int command;
    int module;
    int phoneStatus;
    // ChannelProxy is alive
    boolean isAlive;

    public SyncInfo() {
        super();
    }

    public SyncInfo(int command, int module, int phoneStatus, boolean isAlive) {
        super();
        this.command = command;
        this.module = module;
        this.phoneStatus = phoneStatus;
        this.isAlive = isAlive;
    }

    public int getCommand() {
        return command;
    }

    public void setCommand(int command) {
        this.command = command;
    }

    public int getModule() {
        return module;
    }

    public void setModule(int module) {
        this.module = module;
    }

    public int getPhoneStatus() {
        return phoneStatus;
    }

    public void setPhoneStatus(int phoneStatus) {
        this.phoneStatus = phoneStatus;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }

    public String encode() {
        String codeStr = "";
        try {
            JSONObject codeJson = new JSONObject(makeHeader());
            codeJson.put(
                    TAG_BODY,
                    new JSONObject().put(TAG_COMMAND, command)
                            .put(TAG_MODULE, module)
                            .put(TAG_STATUS, phoneStatus)
                            .put(TAG_ALIVE, isAlive));
            codeStr += codeJson.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return codeStr;
    }

    public boolean decode(String str) {
        try {
            JSONObject json = new JSONObject(str);
            command = json.getInt(TAG_COMMAND);
            module = json.getInt(TAG_MODULE);
            phoneStatus = json.getInt(TAG_STATUS);
            isAlive = json.getBoolean(TAG_ALIVE);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public int getType() {
        // TODO Auto-generated method stub
        return DataFormatBasic.TYPE_SYNC_INFO;
    }

}
